package vampirism.core.client;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import vampirism.core.shared.Network;

public class ClientTextureParser {
	
	private MainGame main;

	public ClientTextureParser(MainGame main){
		this.main = main;
	}
	

	public void getPlayerTextures() {
		
		int tileW = 8;
		int tileH = 8;
		
		int cols = main.spriteSheetPlayer.getWidth() / tileW;
		int rows = main.spriteSheetPlayer.getHeight() / tileH;
		
        TextureRegion[][] tilesPlayer = TextureRegion.split(main.spriteSheetPlayer, tileW, tileH);
        TextureRegion[][] tilesPlayerFlipped = TextureRegion.split(main.spriteSheetPlayer, tileW, tileH);
        
        
        main.framesPlayer = new TextureRegion[cols * rows];
        main.framesPlayerFlipped = new TextureRegion[cols * rows];
        
        int index = 0;
        for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                	main.framesPlayer[index] = tilesPlayer[i][j];
                	main.framesPlayerFlipped[index] = tilesPlayerFlipped[i][j]; main.framesPlayerFlipped[index].flip(true, false);
                	index++;
                }
        }
        
        TextureRegion[] framesRight;
        TextureRegion[] framesLeft;
        TextureRegion[] framesDown;
        TextureRegion[] framesUp;
        
        for(int i = 0; i < 6; i++){
            framesRight = new TextureRegion[2];
            framesRight[0] = main.framesPlayer[0+(21*i)];
            framesRight[1] = main.framesPlayer[1+(21*i)];
            main.aniRightPlayer[i] = new Animation(0.2f,framesRight);
            
            framesLeft = new TextureRegion[2];
            framesLeft[0] = main.framesPlayerFlipped[0+(21*i)];
            framesLeft[1] = main.framesPlayerFlipped[1+(21*i)];
            main.aniLeftPlayer[i] = new Animation(0.2f,framesLeft);
            
            framesDown = new TextureRegion[2];
            framesDown[0] = main.framesPlayer[8+(21*i)];
            framesDown[1] = main.framesPlayer[9+(21*i)];
            main.aniDownPlayer[i] = new Animation(0.2f,framesDown);
            
            framesUp = new TextureRegion[2];
            framesUp[0] = main.framesPlayer[15+(21*i)];
            framesUp[1] = main.framesPlayer[16+(21*i)];
            main.aniUpPlayer[i] = new Animation(0.2f,framesUp);
        }
        

	}
	
	public void getNpcTextures(){
		int tileW = 8;
		int tileH = 8;
		
		int cols = main.spriteSheetNpcSmall.getWidth() / tileW;
		int rows = main.spriteSheetNpcSmall.getHeight() / tileH;
		
        TextureRegion[][] tilesNpc = TextureRegion.split(main.spriteSheetNpcSmall, tileW, tileH);
        TextureRegion[][] tilesNpcFlipped = TextureRegion.split(main.spriteSheetNpcSmall, tileW, tileH);
        
        main.framesNpc = new TextureRegion[cols * rows];
        main.framesNpcFlipped = new TextureRegion[cols * rows];
        
        int index = 0;
        for (int i = 0; i < rows; i++) {
             for (int j = 0; j < cols; j++) {
            	 main.framesNpc[index] = tilesNpc[i][j];
                 main.framesNpcFlipped[index] = tilesNpcFlipped[i][j]; main.framesNpcFlipped[index].flip(true, false);
                 index++;
             }
        }
        
        TextureRegion[] framesRight;
        TextureRegion[] framesLeft;
        
        for(int i = 0; i < main.aniRightNpc.length; i++){
            framesRight = new TextureRegion[2];
            framesRight[0] = main.framesNpc[0+(7*i)];
            framesRight[1] = main.framesNpc[1+(7*i)];
            main.aniRightNpc[i] = new Animation(0.2f,framesRight);
            
            framesLeft = new TextureRegion[2];
            framesLeft[0] = main.framesNpcFlipped[0+(7*i)];
            framesLeft[1] = main.framesNpcFlipped[1+(7*i)];
            main.aniLeftNpc[i] = new Animation(0.2f,framesLeft);
        }
	}
	
	public void getBiomeTextures(){
		
		int tileW = 8;
		int tileH = 8;
		
		TextureRegion[][] tilesBiome = TextureRegion.split(main.spriteSheetBiomes, tileW, tileH);
		
        for (int i = 0; i < Network.MAP_W; i++) {
            for (int j = 0; j < Network.MAP_H; j++) {
            	main.map[i][j].texture = tilesBiome[main.map[i][j].biomeNum][main.rand.nextInt(4)];
            }
        }
	}


	public void getBulletTextures() {
		
		int tileW = 8;
		int tileH = 8;
		
		int cols = main.spriteSheetBullets.getWidth() / tileW;
		int rows = main.spriteSheetBullets.getHeight() / tileH;
		
		TextureRegion[][] tilesProjectile = TextureRegion.split(main.spriteSheetBullets, tileW, tileH);
		
		main.framesBullet = new TextureRegion[cols * rows];
		
        int index = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
               main.framesBullet[index] = tilesProjectile[i][j];
               index++;
            }
        }
	}
}
